"""
@author: chenzhenhua
@project: xltpl
@file: check_table.py
@time: 2021/6/16 0016 12:40
@desc:
"""

import calendar
import datetime

import chinese_calendar
import numpy as np

from xltpl.writerx import BookWriter as BookWriterx


class CheckTable(object):
    def __init__(self, name, num, check):
        self.name = name
        self.num = num
        self.check = check
        self.year = datetime.datetime.now().year
        self.month = datetime.datetime.now().month

    def get_this_day(self, year, month):
        """

        :param year: 年份
        :param month: 月份
        :return: 获取指定月份的天数
        """
        return calendar.monthrange(year, month)

    def get_days(self):
        if self.month == 1:
            self.year_before = self.year - 1
            self.month_before = 12
        else:
            self.year_before = self.year
            self.month_before = self.month - 1

        day1 = self.get_this_day(self.year, self.month)
        day2 = self.get_this_day(self.year_before, self.month_before)
        return day1, day2

    def create_data(self):
        subject_name = []
        subject_num = []
        weekday = []
        day = []
        check_all = []

        week_big = {1: '一',
                    2: '二',
                    3: '三',
                    4: '四',
                    5: '五',
                    6: '六',
                    7: '日'}

        self.workday_num = 0
        self.subject_num_all = 0
        day1, day2 = self.get_days()

        for i in range(21, day2[1]):
            day.append(i)
            week = calendar.weekday(self.year_before, self.month_before, i)
            this_day = datetime.date(self.year_before, self.month_before, i)
            data_is_workday = chinese_calendar.is_workday(this_day)
            weekday.append(week_big[week + 1])
            if data_is_workday:
                self.workday_num = self.workday_num + 1
                subject_name.append(self.name[week])
                self.subject_num_all = self.subject_num_all + self.num[week]
                subject_num.append(self.num[week])
                check_all.append(self.check)
            else:
                subject_name.append('')
                check_all.append('')
                subject_num.append('')

        for i in range(1, 21):
            day.append(i)
            week = calendar.weekday(self.year, self.month, i)
            this_day = datetime.date(self.year, self.month, i)
            data_is_workday = chinese_calendar.is_workday(this_day)
            weekday.append(week_big[week + 1])
            if data_is_workday:
                self.workday_num = self.workday_num + 1
                subject_name.append(self.name[week])
                self.subject_num_all = self.subject_num_all + self.num[week]
                subject_num.append(self.num[week])
                check_all.append(self.check)
            else:
                subject_name.append('')
                check_all.append('')
                subject_num.append('')
        rows = [day, weekday, subject_name, subject_num, check_all]
        rows = np.array(rows).T.tolist()
        return rows

    def write_to_excel(self, path_origin, path_new):
        writer = BookWriterx(path_origin)
        rows = self.create_data()
        check_info = {}
        check_info['rows'] = rows
        check_info['sheet_name'] = '考勤记录表'
        check_info['subject_num'] = self.subject_num_all
        check_info['workday_num'] = self.workday_num
        check_info['month'] = self.month

        payloads = [check_info]
        writer.render_book(payloads=payloads)

        writer.save(path_new)
